package com.lwl.blog.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwl.blog.core.utils.UUIDUtils;

import com.lwl.blog.web.entity.UserEntity;
import com.lwl.blog.web.mapper.UserMapper;
import com.lwl.blog.web.service.UserService;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Date;

/**
 * 帖子用户业务层实现类
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {


    //登陆
    @Override
    public UserEntity login(String username, String password) {
        //1.根据用户名查询是否存在
        QueryWrapper qw = new QueryWrapper();
        qw.eq("username",username);
        UserEntity u = baseMapper.selectOne(qw);
        //2.判断用户是否存在
        if (u == null) {
            return null;
        }

        if (!password.equals(u.getPassword()) || password == null) {
            return null;
        }
        return u;

    }

    //用户注册
    @Override
    public String register(UserEntity u){
        //根据名字查询
        UserEntity us= null;
        try {
            us = baseMapper.selectByUsername(u.getUsername());
            if (us!=null){
                return "用户名已存在";
            }
            //添加默认状态
            u.setStatus(0);
            u.setCreateTime(new Date());
            u.setActivationCode(UUIDUtils.getId());
            u.setType(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //入库
        int save = baseMapper.insert(u);
        if (save>0){
            return "注册成功";
        }
        return "注册失败";
    }

    @Override
    public UserEntity selectActCode(String code) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("activation_code",code);
        return baseMapper.selectOne(qw);
    }

    @Override
    public void updateHeaderUrl(Integer id, String filePath){
        baseMapper.updateHeaderUrl(id,filePath);
    }
}
